<template>
  <el-container class="homme-container">
    <!--头部-->
    <el-header>
      <div>
        <img src="../../assets/logo.png" alt />
        <span>IMS</span>
      </div>
      <el-menu mode="horizontal" background-color="#333744" text-color="#fff" active-text-color="#ffffff" border-bottom="0px">
        <el-menu-item index="1" @click="change_full_screen">
          <i class="el-icon-full-screen" />
        </el-menu-item>
        <el-submenu index="2">
          <template slot="title">
            {{ userInfo.username || 'airvip' }}
            <el-avatar :src="userInfo.avatar || user_avatar_url"></el-avatar>
          </template>
          <el-menu-item index="1-1"> <i class="fas fa-user"></i> 个人中心</el-menu-item>
          <el-menu-item index="1-2" @click="logout"> <i class="fas fa-sign-out-alt"></i> 退出登录</el-menu-item>
        </el-submenu>
      </el-menu>
    </el-header>

    <!--页面主体-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          <i class="fas fa-bars"></i>
        </div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="item.icon ? item.icon : 'fas fa-coins'"></i>
              <!-- 文本 -->
              <span>{{ item.name }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.name }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!--右侧内容主体-->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 侧边栏菜单
      menuList: [],
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: '',
      user_avatar_url: 'http://thirdwx.qlogo.cn/mmopen/exHRuiaLvh0KD3nWzXkaqP8msaHUE409dbpng2byg4bLJymE8HicgaLibvU39qCj6YicE7ico8HC96b85CNNvbUdeZTvD2g3QOxok/132',
      // 全屏专题
      fullscreen: false,
      userInfo: {
        username: '',
        avatar: '',
      },
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      await this.$http
        .get('menu')
        .then((res) => {
          res = res.data
          // console.log(res)
          if (res.code && res.code == 1) {
            // todo
            this.menuList = res.data
          } else {
            return this.$message.error(res.msg || '未知错误')
          }
        })
        .catch((err) => {
          return this.$message.error(err.message)
        })
    },
    // 侧边栏展开与折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存侧边栏激活的菜单
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    change_full_screen() {
      // 全屏切换函数
      const element = document.documentElement
      this.fullscreen = !!(document.fullscreen || document.mozFullScreen || document.webkitIsFullScreen || document.webkitFullScreen || document.msFullScreen)
      console.log(this.fullscreen)
      if (this.fullscreen) {
        if (document.exitFullscreen) {
          document.exitFullscreen()
        } else if (document.webkitCancelFullScreen) {
          document.webkitCancelFullScreen()
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen()
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen()
        }
      } else {
        if (element.requestFullscreen) {
          element.requestFullscreen()
        } else if (element.webkitRequestFullScreen) {
          element.webkitRequestFullScreen()
        } else if (element.mozRequestFullScreen) {
          element.mozRequestFullScreen()
        } else if (element.msRequestFullscreen) {
          // IE11
          element.msRequestFullscreen()
        }
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.homme-container {
  height: 100%;
}

.el-header {
  background-color: #333744;
  display: flex;
  justify-content: space-between;
  padding-left: 10px;
  align-items: center;
  color: #ffffff;
  font-size: 20px;

  .el-menu {
    border-bottom: none !important;
  }

  div {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }

    img {
      height: 40px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.fas {
  margin-right: 5px;
}

.el-menu--horizontal > .el-menu-item.is-active,
.el-menu--horizontal > .el-submenu.is-active .el-submenu__title {
  border-bottom: 0px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #ffffff;
  text-align: center;
  letter-spacing: 3px;
  cursor: pointer;
}
</style>